﻿using NFinalCompiler.SqlModel;
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Threading.Tasks;
using NFinal;

//此代码由NFinalCompiler生成。
//http://bbs.nfinal.com
namespace NFinalCompiler.SqlModel.NetStandard
{
    /// <summary>
    /// 模板类
    /// </summary>
    [View("/NFinalCompiler/SqlModel/NetStandard/Model.cshtml")]
    public class ModelRazor : NFinal.View.RazorView<ModelData>
    {
        /// <summary>
        /// 模板类初始化函数
        /// </summary>
        /// <param name="writer">写对象</param>
        /// <param name="Model">数据</param>
        public ModelRazor(NFinal.IO.Writer writer, ModelData Model) : base(writer, Model)
        {
        }
        /// <summary>
        /// 输出模板内容
        /// </summary>
        public override void Execute()
        {
            this.ExecuteAsync().Wait();
        }
        /// <summary>
        /// 输出模板内容(异步)
        /// </summary>
        public override async Task ExecuteAsync()
        {
            await writer.WriteAsync("");
            await writer.WriteAsync("using System;\r\nusing System.ComponentModel.DataAnnotations;\r\nusing System.ComponentModel.DataAnnotations.Schema;\r\nnamespace ");
            await writer.WriteAsync(Model.ModelNameSpace);
            await writer.WriteAsync(".");
            await writer.WriteAsync(Model.DataBaseShortName);
            await writer.WriteAsync("\r\n{\r\n    /// <summary>\r\n    /// ");
            await writer.WriteAsync(Model.Table.Description);
            await writer.WriteAsync("\r\n    ///</summary>\r\n    [Serializable]\r\n    [DataBase(\"");
            await writer.WriteAsync(Model.DataBaseName);
            await writer.WriteAsync("\",\"");
            await writer.WriteAsync(Model.DataBaseShortName);
            await writer.WriteAsync("\",\"");
            await writer.WriteAsync(Model.DataBaseType);
            await writer.WriteAsync("\")]\r\n    [Table(\"");
            await writer.WriteAsync(Model.Table.Name);
            await writer.WriteAsync("\")]\r\n    public partial class ");
            await writer.WriteAsync(ModelHelper.GetTableName(Model.Table));
            await writer.WriteAsync("Info\r\n    {\r\n        #region 私有变量\r\n");
            foreach (var col in Model.Table.Columns)
            {
                await writer.WriteAsync("            ");
                await writer.WriteAsync("private ");
                await writer.WriteAsync(col.DataType.NetDataTypeCSharpName);
                await writer.WriteAsync("");
                await writer.WriteAsync(col.Nullable && (!col.DataType.IsString) ? "?" : "");
                await writer.WriteAsync(" ");
                await writer.WriteAsync(ModelHelper.GetPrivateName(col.Name));
                await writer.WriteAsync("");
                await writer.WriteAsync(ModelHelper.FiledDefaultValue(col));
                await writer.WriteAsync("///");
                await writer.WriteAsync(col.Description);
                await writer.WriteAsync("\r\n");
            }
            await writer.WriteAsync("        #endregion\r\n\r\n        #region 数据库model\r\n");
            foreach (var col in Model.Table.Columns)
            {
                await writer.WriteAsync("            ");
                await writer.WriteAsync("/// <summary>\r\n            ");
                await writer.WriteAsync("/// ");
                await writer.WriteAsync(col.Description);
                await writer.WriteAsync("\r\n            ");
                await writer.WriteAsync("///</summary>");
                await writer.WriteAsync(col.IsPrimaryKey ? "\r\n            [Key]" : "");
                await writer.WriteAsync("");
                await writer.WriteAsync(col.IsAutoNumber ? "\r\n            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]" : "");
                await writer.WriteAsync("");
                await writer.WriteAsync(col.IsComputed ? "\r\n            [DatabaseGenerated(DatabaseGeneratedOption.Computed)]" : "");
                await writer.WriteAsync("");
                await writer.WriteAsync(col.DataType.NetDataTypeCSharpName == "geography" ? "\r\n            [NotMapped]" : "\r\n            [Column(\"" + col.Name + "\")]");
                await writer.WriteAsync("\r\n            ");
                await writer.WriteAsync("public ");
                await writer.WriteAsync(col.DataType.NetDataTypeCSharpName);
                await writer.WriteAsync("");
                await writer.WriteAsync(col.Nullable && (!col.DataType.IsString) ? "?" : "");
                await writer.WriteAsync(" ");
                await writer.WriteAsync(ModelHelper.GetName(col.Name));
                await writer.WriteAsync("\r\n            ");
                await writer.WriteAsync("{\r\n                ");
                await writer.WriteAsync("set { ");
                await writer.WriteAsync(ModelHelper.GetPrivateName(col.Name));
                await writer.WriteAsync(" = value; }\r\n                ");
                await writer.WriteAsync("get { return ");
                await writer.WriteAsync(ModelHelper.GetPrivateName(col.Name));
                await writer.WriteAsync(" ; }\r\n            ");
                await writer.WriteAsync("}\r\n            ");
                await writer.WriteAsync("\r\n");
            }
            await writer.WriteAsync("        #endregion\r\n    }\r\n}");
        }
    }
}
